<template>
    <div class="main">
        <div class="main-header" v-if="buttons.length">
            <el-button
                v-for="(item, index) in buttons"
                :key="index"
                :type="item.path | formatType"
                :icon="item.icon"
                :disabled="item.path | formatDisabled(selection)"
                @click="tapButton(item)"
                >{{ item.title }}</el-button
            >
        </div>
        <el-table
            ref="table"
            class="main-table"
            :data="fileData"
            height="100%"
            border
            header-cell-class-name="main-table-header"
            @selection-change="onSelectionChange"
            @row-click="onRowClick"
        >
            <el-table-column type="selection" width="39"> </el-table-column>
            <el-table-column prop="admin" align="center" label="上传管理员">
                <template slot-scope="{ row }">{{
                    row.admin.username
                }}</template>
            </el-table-column>
            <el-table-column prop="name" align="center" label="原始文件名">
                <template slot-scope="{ row }">
                    <el-link
                        type="primary"
                        :underline="false"
                        :href="'https://mtang.zxkjnc.com' + row.url"
                        target="__blank"
                        >{{ row.name }}</el-link
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="topic"
                align="center"
                label="类别"
            ></el-table-column>
            <el-table-column
                prop="mimetype"
                align="center"
                label="文件类型"
            ></el-table-column>
            <el-table-column prop="size" align="center" label="文件大小">
                <template slot-scope="{ row }">
                    {{ row.size | formatSizeUnits }}
                </template>
            </el-table-column>
            <el-table-column
                prop="lastuploadtime"
                align="center"
                label="上传时间"
            >
                <template slot-scope="{ row }">
                    {{ row.lastuploadtime | formatDate }}
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            class="main-pagination"
            :current-page.sync="page"
            :page-size="limit"
            :total="total"
            @current-change="onPageChange"
            layout="total,jumper,prev,pager,next"
        ></el-pagination>
        <el-dialog
            :visible.sync="dialogVisible"
            :title="dialogTitle"
            append-to-body
            width="800px"
            @close="onDialogClose"
        >
            <component
                :is="dialogView"
                @update="getFileData"
                :selection="selection"
            ></component>
        </el-dialog>
    </div>
</template>

<script>
import { get } from "@/assets/js/http.js";

export default {
    name: "view-file",
    data() {
        return {
            fileData: [],
            page: 1,
            limit: 20,
            total: 0,
            quick_search: "",
            selection: [],
            dialogVisible: false,
            dialogTitle: "",
            dialogView: "",
            admin: {}
        };
    },
    mounted() {
        document.addEventListener("visibilitychange", () => {
            this.$refs.table.clearSelection();
        });
        this.getFileData();
    },
    components: {
        Detail: () => import("./detail.vue")
        // Edit: () => import("./edit.vue")
    },
    computed: {
        buttons() {
            return this.$store.state.buttons;
        }
    },
    methods: {
        onSearch() {
            this.$prompt("根据关键字搜索", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            })
                .then(({ value }) => {
                    this.quick_search = value;
                    this.page = 1;
                    this.getFileData();
                })
                .catch(() => {});
        },
        onReset() {
            this.quick_search = "";
            this.page = 1;
            this.getFileData();
        },
        onPageChange(index) {
            this.page = index;
            this.getFileData();
        },
        async getFileData() {
            const data = await get({
                url: "/admin/routine.attachment/index",
                loading: true,
                params: {
                    page: this.page,
                    limit: this.limit,
                    quick_search: this.quick_search
                }
            });
            this.fileData = data.list;
            this.total = data.total;
        },
        onCheck() {
            this.dialogTitle = "上传管理员信息";
            this.dialogView = "Detail";
            this.dialogVisible = true;
        },
        onSelectionChange(selection) {
            this.selection = selection;
        },
        onRowClick(row) {
            this.$refs.table.toggleRowSelection(row);
        },
        onDialogClose() {
            this.$refs.table.clearSelection();
        },
        tapButton(item) {
            if (item.path === "check") {
                this.onCheck();
            } else if (item.path === "search") {
                this.onSearch();
            } else if (item.path === "refresh") {
                this.onReset();
            }
        }
    }
};
</script>